package com.xuyuan.thread.threadcorporation;

import com.xuyuan.util.ConsoleColors;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadCorporationExample {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        ServiceExample serviceExample = new ServiceExample();
        executorService.submit(() -> serviceExample.waitMethod());
        executorService.submit(() -> serviceExample.waitMethod());
        executorService.submit(() -> serviceExample.waitMethod());
        executorService.submit(() -> serviceExample.waitMethod());
        executorService.submit(() -> serviceExample.waitMethod());
        Thread.sleep(5000);
        executorService.submit(() -> serviceExample.notifyMethod());
        Thread.sleep(5000);
        executorService.submit(() -> serviceExample.notifyAllMethod());
    }
}

class ServiceExample {
    // 锁对象，使用byte数组，暂用最小资源
    byte[] obj = new byte[1];

    public void waitMethod() {
        synchronized (obj) {
            try {
                System.out.println(ConsoleColors.RED + Thread.currentThread().getName() + "wait begin:" + LocalDateTime.now() + ConsoleColors.RESET);
                // 挂起当前线程
                obj.wait();
                System.out.println(ConsoleColors.RED + Thread.currentThread().getName() + "wait end:" + LocalDateTime.now() + ConsoleColors.RESET);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void notifyMethod() {

        synchronized (obj) {
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " notify random one begin:" + LocalDateTime.now() + ConsoleColors.RESET);
            // 唤起同一锁资源的随机一个线程，加入资源的竞争队列
            obj.notify();
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " notify random one success:" + LocalDateTime.now() + ConsoleColors.RESET);
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " 正在执行当前逻辑" + LocalDateTime.now() + ConsoleColors.RESET);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " 执行完成释放锁资源" + LocalDateTime.now() + ConsoleColors.RESET);
        }
    }

    public void notifyAllMethod() {
        synchronized (obj) {
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " notifyAll begin:" + LocalDateTime.now() + ConsoleColors.RESET);
            // 唤起同一锁资源的所有线程，加入资源的竞争队列
            obj.notifyAll();
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " notifyAll success:" + LocalDateTime.now() + ConsoleColors.RESET);
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " 正在执行当前逻辑" + LocalDateTime.now() + ConsoleColors.RESET);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(ConsoleColors.GREEN + Thread.currentThread().getName() + " 释放锁资源" + LocalDateTime.now() + ConsoleColors.RESET);
        }
    }
}